#{extends 'main.html' /} #{set title:'OsamaPage' /}
<head>
</head>
<script type="text/javascript">
    <!--
    var currrent = "";
    $(function() {
        $.getJSON("@{UserProfile.getId()}",function(data){
            current = data.res;
        });
        $('#createEntityDiv').hide();
        $('#createSubEntityDiv').hide();
        $('#deleteEntityDiv').hide();
        $('#orgRequestDiv').hide();
        $('#devRequestDiv').hide();
        $('#devCanDivInEnt').hide();
    });

    function OnGoMenuFormLink(GoList) {
        var url = GoList.options[GoList.selectedIndex].value;
        //var target = GoList.options[GoList.selectedIndex].className;
        if (url == "CreateEn") {
            //prepare the data which is the organizations he can partiotion it
            document.getElementById("createEntityDiv").innerHTML = "";
            $
            .getJSON(
            '@{Application.getManagedOrganizations()}',
            {
                'id' : current
            },
            function(data) {
                if (data == null || data.length == 0) {
                    alert("You arnot athurized to do that in any org :)");
                } else {
                    alert(data.length)
                    var total = "<p><label>please choose one from your organizations</label><select id=\"selavorg\">"
                    for ( var i = 0; i < data.length; i = i + 1) {
                        total += "<option selected value="+data[i].id+">"
                            + data[i].name + "</option>"
                    }
                    total += "</select></p>";
                    total += "<p><input type=\"text\" id=\"newEntName\"/></p>";
                    total += "<p><a href='#' onclick=\"createEntity()\">submit</a></p>";
                    document.getElementById("createEntityDiv").innerHTML = total;
                    $('#createEntityDiv').toggle(1000);
                    $('#createSubEntityDiv').hide(1000);
                    $('#deleteEntityDiv').hide(1000);
                    $('#orgRequestDiv').hide(1000);
                    $('#devRequestDiv').hide(1000);
                    $('#devCanDivInEnt').hide(1000);
                }
            });

        } else if (url == "CreateSub") {
            $('#createSubEntityDiv').hide(1000);
            document.getElementById("createSubEntityDiv").innerHTML = "";
            $
            .getJSON(
            '@{Application.getManagedEntities()}',
            {
                'id' : current
            },
            function(data) {
                if (data == null || data.length == 0) {
                    alert("You don't manage any entity to add a sub entity in :)");
                } else {
                    var total = "<p><label>please choose one from your organizations</label><select id=\"selavent\">"
                    for ( var i = 0; i < data.length; i = i + 1) {
                        total += "<option selected value="+data[i].id+">"
                            + data[i].name + "</option>"
                    }
                    total += "</select></p>";
                    total += "<p><input type=\"text\" id=\"newSubEntName\"/></p>";
                    total += "<p><a href='#' onclick=\"createSubEntity()\">submit</a></p>";
                    document
                    .getElementById("createSubEntityDiv").innerHTML = total;
                    $('#createEntityDiv').hide(1000);
                    $('#createSubEntityDiv').toggle(1000);
                    $('#deleteEntityDiv').hide(1000);
                    $('#orgRequestDiv').hide(1000);
                    $('#devRequestDiv').hide(1000);
                    $('#devCanDivInEnt').hide(1000);
                }
            });

        } else if (url == "DelEn") {
            $('#deleteEntityDiv').hide(1000);
            document.getElementById("deleteEntityDiv").innerHTML = "";
            $
            .getJSON(
            '@{Application.getManagedEntities()}',
            {
                'id' : current
            },
            function(data) {
                if (data == null || data.length == 0) {
                    alert("You don't manage any entity to delete it :)");
                } else {
                    var total = "<p><label>please choose one from your entities</label><select id=\"selavent2\">"
                    for ( var i = 0; i < data.length; i = i + 1) {
                        total += "<option selected value="+data[i].id+">"
                            + data[i].name + "</option>"
                    }
                    total += "</select></p>";
                    total += "<p><a href='#' onclick=\"delEntity()\">submit</a></p>";
                    document.getElementById("deleteEntityDiv").innerHTML = total;
                    $('#createEntityDiv').hide(1000);
                    $('#createSubEntityDiv').hide(1000);
                    $('#deleteEntityDiv').toggle(1000);
                    $('#orgRequestDiv').hide(1000);
                    $('#devRequestDiv').hide(1000);
                    $('#devCanDivInEnt').hide(1000);
                }
            });
        }else if(url == "CheckInviteOrg"){
            $('#orgRequestDiv').hide();

            $.getJSON("@{Application.getRequestsRecievedForMyOrgz()}",{'id':current},function(data){
                if(data == null || data.length == 0){
                    alert("No New requests sent to join your organizations or your managed Entities");
                }else{
                    document.getElementById("orgRequestDiv").innerHTML = "";
                    for(var i = 0 ; i < data.length ; i=i+1){
                        document.getElementById("orgRequestDiv").innerHTML += "<p>"+data[i].name+"<a href='#' onclick = \"acceptReq('"+data[i].id+"')\"> ACCEPT </a></p>";
                    }
                    $('#createEntityDiv').hide(1000);
                    $('#createSubEntityDiv').hide(1000);
                    $('#deleteEntityDiv').hide(1000);
                    $('#orgRequestDiv').toggle(1000);
                    $('#devRequestDiv').hide(1000);
                    $('#devCanDivInEnt').hide(1000);
                }
            });
        }else if(url == "CheckInviteDev"){
            $('#devRequestDiv').hide();
            $.getJSON("@{Application.getRequestsSentToIdeaDeveloper()}",{'devId':current},function(data){
                if(data == null || data.length == 0){
                    alert("No New requests has been sent to you");
                }else{
                    document.getElementById("devRequestDiv").innerHTML = "";
                    for(var i = 0 ; i < data.length ; i=i+1){
                        document.getElementById("devRequestDiv").innerHTML += "<p>"+data[i].name+"<a href='#' onclick = \"acceptReq('"+data[i].id+"')\"> ACCEPT </a></p>";
                    }
                    $('#createEntityDiv').hide(1000);
                    $('#createSubEntityDiv').hide(1000);
                    $('#deleteEntityDiv').hide(1000);
                    $('#orgRequestDiv').hide(1000);
                    $('#devRequestDiv').toggle(1000);
                    $('#devCanDivInEnt').hide(1000);
                }
            });
        }else if (url == "InviteDevToEntity") {
            $('#devCanDivInEnt').hide(1000);
            document.getElementById("devCanDivInEnt").innerHTML = "";
            $
            .getJSON(
            '@{Application.getManagedEntities()}',
            {
                'id' : current
            },
            function(data) {
                if (data == null || data.length == 0) {
                    alert("You don't manage any entity to join developers in :)");
                } else {
                    //alert(data.length);
                    var total = "<p><label>please choose one from your entities</label><select id=\"selavent3\">"
                    for ( var i = 0; i < data.length; i = i + 1) {
                        total += "<option selected value="+data[i].id+">"
                            + data[i].name + "</option>"
                    }
                    total += "</select></p>";
                    total += "<div id='ideaDevelopers'></div>";
                    total += "<p><a href='#' onclick=\"getDevCanReqInEnt()\">submit</a></p>";
                    document
                    .getElementById("devCanDivInEnt").innerHTML = total;
                    $('#devCanDivInEnt').toggle(1000);
                    $('#createEntityDiv').hide(1000);
                    $('#createSubEntityDiv').hide(1000);
                    $('#deleteEntityDiv').hide(1000);
                    $('#orgRequestDiv').hide(1000);
                    $('#devRequestDiv').hide(1000);
                }
            });


        }
    }

    function getDevCanReqInEnt(){
        var entId = document.getElementById('selavent3').options[document.getElementById('selavent3').selectedIndex].value
        alert(entId+" N ");
        document.getElementById('ideaDevelopers').innerHtml = "";
        $('#ideaDevelopers').hide(100);
        $.getJSON("@{Application.getCanRequestIdeaDevelopersToEntity()}",{'entId':entId},function(data){
            if(data == null || data.length == 0){
                alert("There are no developers you can invite to this entity");
            }else{
                var total = "";
                for(var i = 0 ; i < data.length ; i=i+1){
                    if(data[i].id == -1){
                        total += "<p>"+data[i].name+"</p>";
                    }else{
                        total += "<p>"+data[i].name+" <a href='#' onclick=\"requestDeveloperInEntity('"+data[i].id+"','"+entId+"')\">Invite</a></p>";
                    }
                }
                document.getElementById("ideaDevelopers").innerHTML = total;
                $('#ideaDevelopers').toggle(500);
            }
        });
    }

    function requestDeveloperInEntity(devId,entId){
        $.getJSON("@{Application.requestIdeaDeveloperToEntity()}",{'devId':devId,'entId':entId},function(data){
            alert(data.res);
            getDevCanReqInEnt();
        });
    }
    function acceptReq(reqId){
        $.getJSON("@{Application.acceptRequest()}",{'reqId':reqId},function(data){
            alert(data.res);
        });
    }

    function createEntity() {
        var orgid = document.getElementById('selavorg').options[document
            .getElementById('selavorg').selectedIndex].value
        alert(current + "  " + orgid);
        if (document.getElementById("newEntName").value.length < 3) {
            alert('name req and atleast 3');
        } else {
            //public static void addEntity(String entityName, long orgId, long ideaDeveloper) {
            $.getJSON('@{Application.addEntity()}', {
                'entityName' : document.getElementById("newEntName").value,
                'orgId' : orgid,
                'ideaDeveloper' : current
            }, function(data) {
                alert(data.res);
                if (data.res == "Done Adding") {
                    $('#createEntityDiv').toggle(1000);
                }
            });
        }

    }
    //public static void addSubEntity(String entityName, long entId, long ideaDeveloper)
    function createSubEntity() {
        if (document.getElementById("newSubEntName").value.length < 3) {
            alert('name req and atleast 3');
        } else {
            var orgid = document.getElementById('selavent').options[document
                .getElementById('selavent').selectedIndex].value
            $.getJSON('@{Application.addSubEntity()}', {
                'entityName' : document.getElementById("newSubEntName").value,
                'entId' : orgid,
                'ideaDeveloper' : current
            }, function(data) {
                alert(data.res);
                if (data.res == "Done Adding") {
                    $('#createSubEntityDiv').hide(1000);
                }
            });
        }
    }
    //public static void deleteEntity(long entId, long ideaDeveloper)
    function delEntity() {
        var orgid = document.getElementById('selavent2').options[document
            .getElementById('selavent2').selectedIndex].value;
        $.getJSON('@{Application.deleteEntity()}', {
            'entId' : orgid,
            'ideaDeveloper' : current
        }, function(data) {
            alert(data.res)
            if (data.res == "DONE") {
                $('#deleteEntityDiv').hide(1000);
            }
        });
    }
</script>
</head>
<body>
    <div id="content">
        <div>
            <form name="GoMenuForm0" action="">
                <select id ="GoMenu" name="GoMenu"
                        style=" left: 1px; top: 1px; width: 149px; height: 30px; font-family: 'Courier New'; font-size: 13px;"
                        onchange="OnGoMenuFormLink(this)">
                    <option selected>Select a link</option>
                    <option class="_self" value="CreateEn">Create Entity</option>
                    <option class="_self" value="CreateSub">Create Sub Entity</option>
                    <option class="_self" value="DelEn">Delete Entity</option>
                    <option class="_self" value="CheckInviteOrg">Check
						Invitations Sent To One Of your Organizations Or Entities</option>
                    <option class="_self" value="CheckInviteDev">Check
						Invitations Sent To you by Organizations Or Entities</option>
                    <option class="_self" value="InviteDevToEntity">
						Invite Idea Developer to One Of your Entities</option>
                    <option class="_self" value="InviteDevToOrganization">
						Invite Idea Developer to One Of your Organizations</option>
                </select>

            </form>
        </div>
        <br/>
        <div id="createEntityDiv"></div>

        <div id="createSubEntityDiv"></div>

        <div id="deleteEntityDiv"></div>

        <div id="orgRequestDiv"></div>

        <div id="devRequestDiv"></div>

        <div id="devCanDivInEnt"></div>

        <div id="devCanDivInOrg"></div>
    </div>
    <div id="sidebar"></div>
</body>
